 import LightSystem from './light-system.js'
 
 import { Vector3, Box3, OrthographicCamera, PerspectiveCamera } from 'three';

// 常量配置
const CAMERA_DEFAULTS = {
  ORTHOGRAPHIC: {
    near: 0.1,
    far: 5000,
    defaultPositionZ: 1000,
  },
  PERSPECTIVE: {
    near: 0.1,
    far: 5000,
    fov: 45,
  }
};

class CameraSystem extends LightSystem {
  constructor(options = {}) {
    super(options);
    // 初始化相机目标点
    this.cameraTarget = new Vector3();
  }

  // 计算视图尺寸
  #calculateViewSize(objectSize, canvasAspect) {
    const objectAspect = objectSize.x / objectSize.y;
    return objectAspect > canvasAspect 
      ? objectSize.x / canvasAspect  // 宽屏适配
      : objectSize.y;                // 竖屏适配
  }

  // 创建基础正交相机
  #createBaseOrthographicCamera(viewSize, canvasAspect, config) {
    const halfW = (viewSize * canvasAspect) / 2;
    const halfH = viewSize / 2;
    
    return new OrthographicCamera(
      -halfW,
      halfW,
      halfH,
      -halfH,
      config.near ?? CAMERA_DEFAULTS.ORTHOGRAPHIC.near,
      config.far ?? CAMERA_DEFAULTS.ORTHOGRAPHIC.far
    );
  }

  // 文本适配相机
  createTextCamera(textMesh, config = {}) {
    const canvasSize = this.size;
    const bbox = new Box3().setFromObject(textMesh);
    const objectSize = new Vector3();
    
    bbox.getSize(objectSize);
    const center = new Vector3();
    bbox.getCenter(center);

    // 动态计算视图尺寸
    const viewSize = this.#calculateViewSize(objectSize, canvasSize.width / canvasSize.height);
    
    // 创建相机
    const camera = this.#createBaseOrthographicCamera(
      viewSize,
      canvasSize.width / canvasSize.height,
      config
    );

    // 计算相机位置
    const baseZ = config.positionZ ?? CAMERA_DEFAULTS.ORTHOGRAPHIC.defaultPositionZ;
    const position = new Vector3(
      center.x,
      center.y,
      Math.max(baseZ, objectSize.length() * 1.5) // 动态调整Z位置
    );

    camera.position.copy(position);
    camera.lookAt(center);
    return camera;
  }

  // 创建配置化相机
  createCamera(options = {}) {
    const config=options.camera;
    const { type = "Orthographic" } = config;
    const size = this.size;
    
    if (type === "Orthographic") {
      return this.#createOrthographicCamera(config, size);
    }
    return this.#createPerspectiveCamera(config, size);
  }

  // 创建正交相机（私有方法）
  #createOrthographicCamera(config, canvasSize) {
     
    const { 
      viewSize = config.imageSize || 1000,
      position = new Vector3(0, 0, CAMERA_DEFAULTS.ORTHOGRAPHIC.defaultPositionZ),
      ...orthoConfig
    } = config.orthographic || {};

    const camera = this.#createBaseOrthographicCamera(
      viewSize,
      canvasSize.width / canvasSize.height,
      orthoConfig
    );

    camera.position.copy(position);
    
    camera.lookAt(this.cameraTarget);
    return camera;
  }

  // 创建透视相机（私有方法）
  #createPerspectiveCamera(config, canvasSize) {
    const {
      fov = CAMERA_DEFAULTS.PERSPECTIVE.fov,
      position = new Vector3(0, 0, 10),
      ...perspectiveConfig
    } = config.perspective || {};

    const camera = new PerspectiveCamera(
      fov,
      canvasSize.width / canvasSize.height,
      perspectiveConfig.near ?? CAMERA_DEFAULTS.PERSPECTIVE.near,
      perspectiveConfig.far ?? CAMERA_DEFAULTS.PERSPECTIVE.far
    );

    camera.position.copy(position);
     // 默认看向原点
    camera.lookAt(this.cameraTarget);
    return camera;
  }
}

export default CameraSystem;